package com.belinked_client.room_chat;
import com.client.R;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.GestureDetector;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.OnGestureListener;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

public class Panel_bottomToTop extends LinearLayout{
	
	public interface PanelClosedEvent {
		void onPanelClosed(View panel);
	}
	
	public interface PanelOpenedEvent {
		void onPanelOpened(View panel);
	}
	private final static int HANDLE_WIDTH=70;
	private final static int MOVE_WIDTH=20;
	private ImageButton btnHandle;
	private LinearLayout panelContainer;
	private int mButtomMargin=0;
	private Context mContext;
	private PanelClosedEvent panelClosedEvent=null;
	private PanelOpenedEvent panelOpenedEvent=null;


	public Panel_bottomToTop(Context context,View otherView,int width,int height) {
		super(context);
		this.mContext=context;
		LayoutParams otherLP=(LayoutParams) otherView.getLayoutParams();
		otherLP.weight=1;
		otherView.setLayoutParams(otherLP);
		
		
		this.setBackgroundResource(R.drawable.chat_menu_background);
		 
		LayoutParams lp=new LayoutParams(width, height);
		lp.bottomMargin=-lp.height+HANDLE_WIDTH;
		mButtomMargin=Math.abs(lp.bottomMargin);
		this.setLayoutParams(lp);
		this.setOrientation(LinearLayout.VERTICAL);
		
		btnHandle=new ImageButton(context);
		Drawable d = getResources().getDrawable(R.drawable.selector_room_chat_menu_btn);
		Drawable b = getResources().getDrawable(R.drawable.selector_background);
		btnHandle.setImageDrawable(d);///////////asdfasdfasdf
		btnHandle.setBackgroundDrawable(b);
		btnHandle.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View arg0) {
				LayoutParams lp = (LayoutParams) Panel_bottomToTop.this.getLayoutParams();
				if (lp.bottomMargin < 0)
					new AsynMove().execute(new Integer[] { MOVE_WIDTH });
				else if (lp.bottomMargin >= 0)
					new AsynMove().execute(new Integer[] { -MOVE_WIDTH });
			}
			
		});
		//btnHandle.setOnTouchListener(HandleTouchEvent);
		
		btnHandle.setLayoutParams(new LayoutParams(150,LayoutParams.WRAP_CONTENT));
		
		this.addView(btnHandle);
		
		LayoutParams lp2 = (LayoutParams) btnHandle.getLayoutParams();
		lp2.leftMargin = (int)((width - lp2.width)/2.0f)-5;
	
		panelContainer=new LinearLayout(context);
		panelContainer.setOrientation(LinearLayout.HORIZONTAL);
		panelContainer.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.FILL_PARENT));
		
		this.addView(panelContainer);
	}
	public void close(){
		new AsynMove().execute(new Integer[] { -MOVE_WIDTH });
	}
	public void setPanelClosedEvent(PanelClosedEvent event)
	{
		this.panelClosedEvent=event;
	}


	
	public void setPanelOpenedEvent(PanelOpenedEvent event)
	{
		this.panelOpenedEvent=event;
	}


	public void fillPanelContainer(View v)
	{
		panelContainer.addView(v);
	}


	class AsynMove extends AsyncTask<Integer, Integer, Void> {

		@Override
		protected Void doInBackground(Integer... params) {
			int times;
			if (mButtomMargin % Math.abs(params[0]) == 0)
				times = mButtomMargin / Math.abs(params[0]);
			else
				
				times = mButtomMargin / Math.abs(params[0]) + 1;

			for (int i = 0; i < times; i++) {
				publishProgress(params);
				try {
					Thread.sleep(Math.abs(params[0]));
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			return null;
		}

		@Override
		protected void onProgressUpdate(Integer... params) {
			LayoutParams lp = (LayoutParams) Panel_bottomToTop.this.getLayoutParams();
			if (params[0] < 0)
				lp.bottomMargin = Math.max(lp.bottomMargin + params[0],
						(-mButtomMargin));
			else
				lp.bottomMargin = Math.min(lp.bottomMargin + params[0], 0);

			if(lp.bottomMargin==0 && panelOpenedEvent!=null){
				panelOpenedEvent.onPanelOpened(Panel_bottomToTop.this);
			}
			else if(lp.bottomMargin==-(mButtomMargin) && panelClosedEvent!=null){
				panelClosedEvent.onPanelClosed(Panel_bottomToTop.this);
			}
			Panel_bottomToTop.this.setLayoutParams(lp);
		}
	}

}
